<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="单月累计充值开始金额" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="单月累计充值开始金额">
          <a-input-number
            :min="0.01"
            v-decorator="['start_money', {rules: [{required: true, message: '请输入单月累计充值开始金额'}]}]"
          />
          <span class="ml-10">元</span>
        </a-form-item>
		<a-form-item label="单月累计充值结束金额" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="单月累计充值结束金额">
		  <a-input-number
		    :min="0.01"
		    v-decorator="['end_money', {rules: [{required: true, message: '请输入单月累计充值结束金额'}]}]"
		  />
		  <span class="ml-10">元</span>
		</a-form-item>
		<a-form-item label="奖励比例" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="单月累计充值到达开始金额奖励充值金额的比例">
		  <a-input-number
		    :min="0.01"
		    v-decorator="['reward_ratio', {rules: [{required: true, message: '请输入奖励比例'}]}]"
		  />
		  <span class="ml-10">百分比（%）</span>
		</a-form-item>
		<a-form-item
		  label="经销商等级"
		  :labelCol="labelCol"
		  :wrapperCol="wrapperCol"
		  extra="经销商等级单月累计充值到达开始金额奖励金额，用于核销订单"
		>
		  <a-select 
		  label-in-value
		  v-decorator="['grade', { rules: [{required: true, message: '请选择经销商等级'}] }]">
		    <a-select-option
		      v-for="(item, index) in gradeList"
		      :key="index"
			  :label="item.name"
		      :value="item.grade_id"
		    >{{ item.name }}</a-select-option>
		  </a-select>
		</a-form-item>
        <!-- <a-form-item
          label="赠送金额"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          extra="充值成功后赠送的金额，不能大于充值金额"
        >
          <a-input-number
            :min="0.01"
            v-decorator="['gift_money', {rules: [{required: true, message: '请输入赠送的金额'}]}]"
          />
          <span class="ml-10">元</span>
        </a-form-item> -->
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="数字越小越靠前">
          <a-input-number
            :min="0"
            v-decorator="['sort', {initialValue: 100, rules: [{required: true, message: '请输入排序值'}]}]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/market/recharge/kpiplan'
import * as GradeApi from '@/api/user/grade'

export default {
  data () {
    return {
      // 对话框标题
      title: '编辑业绩奖励设置',
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: { span: 13 },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {},
	  // 等级列表
	  gradeList: []
    }
  },
  methods: {

    /**
     * 显示对话框
     */
    edit (record) {
      // 显示窗口
      this.visible = true
      // 当前记录
      this.record = record
	  // 获取经销商等级列表
	  this.getGradeList()
      // 设置默认值
      this.setFieldsValue()
    },
	// 获取经销商等级列表
	getGradeList () {
	  GradeApi.all().then(result => {
	    this.gradeList = result.data.list
	  })
	},
    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(pick(this.record, ['start_money', 'end_money', 'reward_ratio','grade', 'sort']))
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
      this.form.resetFields()
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      this.confirmLoading = true
      Api.edit({ planId: this.record.plan_id, form: values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally(() => {
          this.confirmLoading = false
        })
    }

  }
}
</script>
